package chapter1.fundamentals.exercise;

import edu.princeton.cs.algs4.StdIn;

import java.util.Arrays;

import static edu.princeton.cs.algs4.StdRandom.uniform;

/**
 * @author quanlinlin on 2018/11/3 23:08.
 * @version 1.0
 */
public class E_36 {

    public static void main(String[] args) {
        int M = StdIn.readInt();
        int N = StdIn.readInt();
        int[] arr = new int[M];
        int[][] result = new int[M][M];
        for (int i = 0; i < N; i++) {
            Arrays.setAll(arr, j -> j);
            shuffle(arr);
            for (int j = 0; j < arr.length; j++) {
                result[arr[j]][j]++;
            }
        }

        for (int i = 0; i < result.length; i++) {
            if (i == 0) {
                System.out.printf("%4s\t", "----");
            }
            System.out.printf("%2d\t", i);
        }
        System.out.println();

        for (int i = 0; i < result.length; i++) {
            System.out.printf("|%2d|\t", i);
            for (int j = 0; j < result[0].length; j++) {
                System.out.printf("%2d\t", result[i][j]);
            }
            System.out.println();
        }

    }

    private static void shuffle(int[] arr) {
        if (arr == null) {
            throw new IllegalArgumentException("argument array is null");
        }
        int n = arr.length;
        for (int i = 0; i < n; i++) {
            int r = uniform(n);     // between i and n-1
            int temp = arr[i];
            arr[i] = arr[r];
            arr[r] = temp;
        }
        //StdRandom.shuffle();
    }


}
